/*
    5. Write a C program to reverse digits of a given a 32-bit signed integer.
    Expected Output:

    Original integer:          123
    Reverse integer :          321
*/

#include <stdio.h>

int getLenOfInteger(int n){
    int num = 1;
    while((n = n / 10)){
        num++;
    }
    return num;
}

int getNumOfInteger(int n, int idx){
    int i = 0;
    while(i++ < idx){
        n = n / 10;
    }
    return n % 10;
}

int reverseInteger(int num){
    int i, j;
    int len = getLenOfInteger(num);
    int ret = 0, n;
    for(i = 0; i < len; i++){
        n = getNumOfInteger(num, i);
        j = len - i - 1;
        while(j--){
            n = n * 10;
        }
        ret = ret + n;
    }
    return ret;
}

int main(void)
{
    int n = 12345;
    printf("len = %d\n", getLenOfInteger(n));
    printf("num = %d\n", getNumOfInteger(n, 3));
    printf("Original integer: %d\n", n);
    printf("Reverse integer : %d\n", reverseInteger(n));

    return 0;
}
